#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: title05优选核酸检测点.py 
@version:
@time: 2023/05/30

https://dream.blog.csdn.net/article/details/128995241

H1 M1 当前时间小时数，当前时间分钟数
H2 M2 指定完成核酸的小时数，指定完成核酸的分钟数
N 所有核酸检测点个数
ID1 D1 C1 核酸点ID值，距离张三的距离，当前排队人数
ID2 D2 C2
...
IDn Dn Cn


10 30
14 50
3
1 10 19
2 8 20
3 21 3



输出
N 满足要求的核酸检测点个数
I2 T2 M2 核酸点id，做完花费时间，做完花费的费用
I3 T3 M3

2
2 80 80
1 190 100

"""
class Point:
    def __init__(self, id, dis, que):
        self.id = id
        self.dis = dis
        self.que = que
        self.pay = dis * 10
        self.time = get_time(start, self)


add8_10 = [3, 480, 600]
add12_14 = [10, 720, 840]


def get_time(start, point):
    time = 0
    time += point.dis * 10
    point.que = max(0, point.que - point.dis * 10)
    on = start + point.dis * 10
    if on <= add8_10[1]:
        return add8_10[1] - start
    if on <= add8_10[2]:
        point.que += (on - add8_10[1]) * add8_10[0] - (on - add8_10[1])
        return time + point.que
    if on <= add12_14[1]:
        return time + point.que
    if on <= add12_14[2]:
        point.que += (on - add12_14[1]) * add12_14[0] - (on - add12_14[1])
        return time + point.que
    return time + point.que


def solve_method(start, end, points):
    res = [p for p in points if start + p.time < end]
    res.sort(key=lambda x: (x.time, x.pay))
    print(len(res))
    for p in res:
        print(p.id, p.time, p.pay)


if __name__ == '__main__':
    t1 = input().strip().split(' ')
    start = int(t1[0]) * 60 + int(t1[1])
    t2 = input().strip().split(' ')
    end = int(t2[0]) * 60 + int(t2[1])

    n = int(input().strip())
    points = [Point(*map(int, input().strip().split())) for _ in range(n)]
    solve_method(start, end, points)

